package com.hbnu.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.AchievementMapper;
import com.hbnu.system.mapper.AchievementQualificationMapper;
import com.hbnu.system.model.context.BaseContext;
import com.hbnu.system.model.entity.*;
import com.hbnu.system.service.*;
import com.hbnu.system.utils.CheckRoleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * (AchievementQualification)表服务实现类
 *
 * @author makejava
 * @since 2025-07-22 22:24:49
 */
@Service
public class AchievementQualificationServiceImpl extends ServiceImpl<AchievementQualificationMapper, AchievementQualification> implements AchievementQualificationService {
    @Autowired
    private AchievementService iAchievementService;

    @Autowired
    private AchievementTypeService iAchievementTypeService;

    @Autowired
    private IPatentTypeService iPatentTypeService;

    @Autowired
    private AchievementQualificationMapper achievementQualificationMapper;

    @Autowired
    private AchievementMapper achievementMapper;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private CheckRoleUtil checkRoleUtil;

    @Autowired
    private AchievementAuthorService iAchievementAuthorService;

    /**
     * 添加科研平台量化分数
     * @param achievementqualification
     * @return
     */
    @Override
    public boolean addAchievementQualification(AchievementQualification achievementqualification) {
//        System.out.println("achievementqualification"+achievementqualification);
        Achievement achievement = iAchievementService.getAchievementById(achievementqualification.getAchievementId());
//        Achievement checkAchievement = BeanUtil.copyProperties(achievement, Achievement.class);
        if(Objects.isNull(achievement)){
            throw new BaseException("科研平台编号有误!");
        }
        System.out.println("check="+achievement.getStatus());
        checkRoleUtil.checkPermissionAchievement(achievement.getStatus());

        if(Objects.isNull(achievement)){
            throw new BaseException("科研平台编号有误!");
        }
        if(Objects.nonNull(this.getOne(Wrappers.lambdaQuery(AchievementQualification.class)
                .eq(AchievementQualification::getAchievementId,achievementqualification.getAchievementId())))){
            throw new BaseException("已经存在量化数据!");
        }
        if(String.valueOf(achievementqualification.getType1ID()).equals("")){
            throw new BaseException("请完整填写数据!");
        }
        if (String.valueOf((achievementqualification.getType2ID())).equals("")){
            throw new BaseException("请完整填写数据!");
        }
        if (String.valueOf((achievementqualification.getType3ID())).equals("")){
            throw new BaseException("请完整填写数据!");
        }

        if(achievementqualification.getManualScoreMode()=="true"){
            achievement.setScore(String.valueOf(achievementqualification.getFinalValue()));
            achievementqualification.setFinalValue(achievementqualification.getFinalValue());
            achievementqualification.setNote(achievementqualification.getNote());
        }else {
//            System.out.println("11111111111111111");
            AchievementType achievementtype = iAchievementTypeService.getOne(Wrappers.lambdaQuery(AchievementType.class)
                    .eq(AchievementType::getType1Id, achievementqualification.getType1ID())
                    .eq(AchievementType::getType2Id, achievementqualification.getType2ID())
                    .eq(AchievementType::getType3Id, achievementqualification.getType3ID()));
//            System.out.println("achievementtype="+achievementtype);
            float value = achievementtype.getValue();
            System.out.println("value"+value);
            achievement.setScore(String.valueOf(value));

            achievementqualification.setFinalValue(value);
            achievementqualification.setAchievementType1(achievementtype.getAchievementType1());
            achievementqualification.setAchievementType2(achievementtype.getAchievementType2());
            achievementqualification.setAchievementType3(achievementtype.getAchievementType3());
//            System.out.println("achievementqualification="+achievementqualification);
        }

        iAchievementService.updateAchievement(achievement);

        String Pscore = "0.00";
        // 9. 修改著作作者的个人得分
        String achievementid=achievementqualification.getAchievementId();
        List<AchievementAuthor> achievementauthorList = iAchievementAuthorService.listAuthor(achievementid);
        String userId = BaseContext.getCurrentId();
        User user = iUserService.getById(userId);
        if (achievementauthorList != null && !achievementauthorList.isEmpty()) {
            boolean shouldUpdate = true;
            if (achievementauthorList.size() == 1) {
                AchievementAuthor soleAuthor = achievementauthorList.get(0);
                if (Objects.equals(soleAuthor.getUserAccount(), user.getAccount())) {
                    shouldUpdate= false;
                }
            }
            // 执行更新操作
            if (shouldUpdate) {
                for (AchievementAuthor author : achievementauthorList) {
                    iAchievementAuthorService.updateAchievementAuthor(author);
                }
            }
        }

        return baseMapper.insert(achievementqualification) == 1;
    }

    /**
     * 删除科研平台量化分数
     * @param AchievementQuantificationID
     * @return
     */
    @Override
    public boolean deleteAchievementQualification(String AchievementQuantificationID) {

        AchievementQualification achievementqualification = baseMapper.selectOne(Wrappers.lambdaQuery(AchievementQualification.class)
                .eq(AchievementQualification::getAchievementQuantificationId, AchievementQuantificationID));
        Achievement p1=iAchievementService.getAchievementById(achievementqualification.getAchievementId());
        checkRoleUtil.checkPermissionAchievement(p1.getStatus());
        if (Objects.isNull(achievementqualification)){
            throw new BaseException("量化编号有误");
        }

        Achievement achievement = iAchievementService.getAchievementById(achievementqualification.getAchievementId());

        achievement.setScore(String.valueOf(0));

//        Achievement achievement = new Achievement();
//        BeanUtils.copyProperties(achievement,achievement);
        iAchievementService.updateAchievement(achievement);

        String Pscore = "0.00";
        // 9. 修改著作作者的个人得分
        String achievementid=achievementqualification.getAchievementId();
        List<AchievementAuthor> achievementauthorList = iAchievementAuthorService.listAuthor(achievementid);
        String userId = BaseContext.getCurrentId();
        User user = iUserService.getById(userId);
        if (achievementauthorList != null && !achievementauthorList.isEmpty()) {
            boolean shouldUpdate = true;
            if (achievementauthorList.size() == 1) {
                AchievementAuthor soleAuthor = achievementauthorList.get(0);
                if (Objects.equals(soleAuthor.getUserAccount(), user.getAccount())) {
                    shouldUpdate= false;
                }
            }
            // 执行更新操作
            if (shouldUpdate) {
                for (AchievementAuthor author : achievementauthorList) {
                    iAchievementAuthorService.updateAchievementAuthor(author);
                }
            }
        }


        return baseMapper.deleteById(AchievementQuantificationID) == 1;
    }


    /**
     * 查询单个科研平台的量化分数
     * @param AchievementID
     * @return
     */
    @Override
    public List<AchievementQualification> getOneAchievementQualification(String AchievementID) {
        Achievement achievement = iAchievementService.getAchievementById(AchievementID);
        if(Objects.isNull(achievement)){
            throw new BaseException("科研平台编号有误!");
        }

        List<AchievementQualification> achievementqualifications = baseMapper.selectList(Wrappers.lambdaQuery(AchievementQualification.class)
                .eq(AchievementQualification::getAchievementId, AchievementID));
        return achievementqualifications;
    }

    /**
     * 更新科研平台分数
     * @param achievementqualification
     * @return
     */
    @Override
    public boolean upAchievementScore(AchievementQualification achievementqualification){
        Achievement p1=iAchievementService.getAchievementById(achievementqualification.getAchievementId());
        checkRoleUtil.checkPermissionAchievement(p1.getStatus());
        AchievementQualification p=baseMapper.selectOne(Wrappers.lambdaQuery(AchievementQualification.class)
                .eq(AchievementQualification::getAchievementQuantificationId,achievementqualification.getAchievementQuantificationId()));
        p.setFinalValue(achievementqualification.getFinalValue());
        //更新achievement表的科研平台分数
        Achievement achievement=achievementMapper.selectOne(Wrappers.lambdaQuery(Achievement.class)
                .eq(Achievement::getId,achievementqualification.getAchievementId()));
        achievement.setScore(String.valueOf(achievementqualification.getFinalValue()));
        iAchievementService.updateAchievement(achievement);

        String Pscore = "0.00";
        // 9. 修改著作作者的个人得分
        String achievementid=achievementqualification.getAchievementId();
        List<AchievementAuthor> achievementauthorList = iAchievementAuthorService.listAuthor(achievementid);
        String userId = BaseContext.getCurrentId();
        User user = iUserService.getById(userId);
        if (achievementauthorList != null && !achievementauthorList.isEmpty()) {
            boolean shouldUpdate = true;
            if (achievementauthorList.size() == 1) {
                AchievementAuthor soleAuthor = achievementauthorList.get(0);
                if (Objects.equals(soleAuthor.getUserAccount(), user.getAccount())) {
                    shouldUpdate= false;
                }
            }
            // 执行更新操作
            if (shouldUpdate) {
                for (AchievementAuthor author : achievementauthorList) {
                    iAchievementAuthorService.updateAchievementAuthor(author);
                }
            }
        }

        return baseMapper.updateById(p)==1;
    }
}

